#ifndef __OS_TIMER_H__
#define __OS_TIMER_H__
#include "os_include.h"
// #include "kal_general_types.h"
/* modify by huc 20230605
FH8626 do not open 
Issue:more than one timer expire the same time , only one usr_cb will be call


man sigevent:

Among the implementation possibilities here  are  that  each  timer  notification
could  result in the creation of a new thread, or that a single
thread is created to receive all notifications

*/
#if  defined(__IC_FH8626__) && defined(__LINUX_OS__)
#define LINUX_OS_TIME_API_V2   0
#else
#define LINUX_OS_TIME_API_V2   1
#endif
typedef void (*kal_timer_func_ptr)(void *param_ptr);
#if LINUX_OS_TIME_API_V2

#include <stdint.h>
#include <stdbool.h>

#include <signal.h>
#include <time.h>
#include <pthread.h>

#define MYTIMER_MAME_LEN_MAX   16

struct my_Timer{
	char  pname[MYTIMER_MAME_LEN_MAX];
	//char * pname;

	rt_timer_t tid;
	kal_timer_func_ptr callfuntion;
	void *	param_ptr;
	
};

typedef struct my_Timer  TimerId_t;
typedef struct my_Timer  *TimerId_id;

struct once_Timer{
	timer_t tid;
	kal_timer_func_ptr callfuntion;
	void *	param_ptr;
};
typedef struct once_Timer  *OnecTimer_id;

extern TimerId_id mytimer_create(const char *pname);
extern int mytimer_open(TimerId_id tid,unsigned int secs,unsigned int ms,
	kal_timer_func_ptr cfun,void *param,bool isauto);
extern int mytimer_open_v2(TimerId_id tid,unsigned int ms,
	kal_timer_func_ptr cfun,void *param,bool isauto);

extern int mytimer_open_us(TimerId_id tid,long us,kal_timer_func_ptr cfun,void *param,bool isauto);

extern int mytimer_close(TimerId_id tid);
extern int mytimer_free(TimerId_id *tid);
extern int mytimer_is_enable(TimerId_id tid);


extern OnecTimer_id once_timer_create(unsigned int secs,unsigned int ms,kal_timer_func_ptr cfun,void *param);
extern void once_timer_free(OnecTimer_id tid);



#else


#if defined(__LINUX_OS__)


#include <stdlib.h>

typedef enum
{
    TIMER_SINGLE_SHOT = 0,
    TIMER_PERIODIC,
    TIMER_DISABLE,
} t_timer;

int     mytimer_initialize();
void    mytimer_finalize();
/////////////////////////////////////////////////////////

#define MYTIMER_MAME_LEN_MAX   8//RT_NAME_MAX // 8 RT_NAME_MAX

struct my_Timer
{
    int                 fd;
    char  pname[MYTIMER_MAME_LEN_MAX];


    kal_timer_func_ptr callfuntion;
    void *	param_ptr;
    unsigned int        interval;
    t_timer             type;
    struct my_Timer * next;
};
typedef struct my_Timer  *TimerId_id;




#else//#if defined(__LINUX_OS__)


#include "config.h"
#include "glable_include.h"


#include <stdint.h>
#include <stdbool.h>





//typedef void (*kal_timer_func_ptr)(void *param_ptr);

#define MYTIMER_MAME_LEN_MAX   RT_NAME_MAX // 8 RT_NAME_MAX

struct my_Timer
{
    char  pname[MYTIMER_MAME_LEN_MAX];

#if defined(__FreeRTOS__)

#if defined(ENABLE_MYTIMER_HW)
    int          tid;
#else
    TimerHandle_t tid;
#endif

#else
    timer_handle_t   tid;
#endif


    kal_timer_func_ptr callfuntion;
    void *	param_ptr;
};

typedef struct my_Timer  TimerId_t;


struct once_Timer
{
    timer_handle_t tid;
    kal_timer_func_ptr callfuntion;
    void *	param_ptr;
};
typedef struct once_Timer  *OnecTimer_id;
typedef struct my_Timer  *TimerId_id;


extern int mytimer_open(TimerId_id tid, unsigned int secs, unsigned int ms,
                        kal_timer_func_ptr cfun, void *param, bool isauto);


extern int mytimer_open_task(TimerId_id tid, unsigned int ms,
                             kal_timer_func_ptr cfun, void *param, bool isauto);





#if defined(ENABLE_FREERTOS_SYSAPI)
extern int mytimer_open_isr(TimerId_id tid, unsigned int secs, unsigned int ms,
                            kal_timer_func_ptr cfun, void *param, bool isauto);
extern int mytimer_open_v2_isr(TimerId_id tid, unsigned int ms,
                               kal_timer_func_ptr cfun, void *param, bool isauto);

extern int mytimer_close_isr(TimerId_id tid);
#else
#define mytimer_open_isr  			mytimer_open
#define mytimer_open_v2_isr			mytimer_open_v2
#define mytimer_close_isr			mytimer_close
#endif

//extern OnecTimer_id once_timer_create(unsigned int secs,unsigned int ms,kal_timer_func_ptr cfun,void *param);
//extern void once_timer_free(OnecTimer_id tid);

#if defined(__FreeRTOS__)
extern int my_HW_timer_start(UINT32 tick, PVOID pfun, void *param, BOOL isAuto);
extern int my_HW_timer_stop(int timerID);
#endif

#if defined(__RTT_OS__)
extern TimerId_id mytimer_hw_create(const char *pname);
#endif

#endif






extern int mytimer_close(TimerId_id tid);
extern int mytimer_free(TimerId_id *tid);
extern TimerId_id mytimer_create(const char *pname);
extern int mytimer_open_us(TimerId_id tid, long us, kal_timer_func_ptr cfun, void *param, bool isauto);
extern int mytimer_open_v2(TimerId_id tid, unsigned int ms,	kal_timer_func_ptr cfun, void *param, bool isauto);
extern int mytimer_open(TimerId_id tid, unsigned int secs, unsigned int ms,
                        kal_timer_func_ptr cfun, void *param, bool isauto);

#endif

#endif
